package cc.rengu.igas.bsps.biz.service.impl;

import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.facade.MchntInspectService;
import cc.rengu.igas.bsps.facade.request.*;
import cc.rengu.igas.bsps.facade.response.*;
import cc.rengu.igas.bsps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 商户巡检服务实现类
 * Created by 王成 on 2021/6/18.
 */
public class MchntInspectServiceImpl implements MchntInspectService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MchntInspectTaskQueryResponse> mchntInspectTaskQuery(MchntInspectTaskQueryRequest mchntInspectTaskQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntInspectTaskQueryResponse> bspResult = new Result<>();
        MchntInspectTaskQueryResponse mchntInspectTaskQueryResponse = new MchntInspectTaskQueryResponse();
        mchntInspectTaskQueryResponse.setHeader(mchntInspectTaskQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntInspectTaskQueryRequest, MchntInspectTaskQueryResponse.class);
            mchntInspectTaskQueryResponse = (MchntInspectTaskQueryResponse) dubboResult.getResult();
            mchntInspectTaskQueryResponse.setRespCode(dubboResult.getRespCode());
            mchntInspectTaskQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户巡检任务详情查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntInspectTaskQueryResponse.setRespCode(((BizException) e).getCode());
                mchntInspectTaskQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntInspectTaskQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntInspectTaskQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntInspectTaskQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntSelfInspectResponse> mchntSelfInspect(MchntSelfInspectRequest mchntSelfInspectRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntSelfInspectResponse> bspResult = new Result<>();
        MchntSelfInspectResponse mchntSelfInspectResponse = new MchntSelfInspectResponse();
        mchntSelfInspectResponse.setHeader(mchntSelfInspectRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntSelfInspectRequest, MchntSelfInspectResponse.class);
            mchntSelfInspectResponse = (MchntSelfInspectResponse) dubboResult.getResult();
            mchntSelfInspectResponse.setRespCode(dubboResult.getRespCode());
            mchntSelfInspectResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户自助巡检失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntSelfInspectResponse.setRespCode(((BizException) e).getCode());
                mchntSelfInspectResponse.setRespDesc(e.getMessage());
            } else {
                mchntSelfInspectResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntSelfInspectResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntSelfInspectResponse);
        return bspResult;
    }

    @Override
    public Result<NeedInspectMchntStatisQueryResponse> needInspectMchntStatisQuery(NeedInspectMchntStatisQueryRequest needInspectMchntStatisQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NeedInspectMchntStatisQueryResponse> bspResult = new Result<>();
        NeedInspectMchntStatisQueryResponse needInspectMchntStatisQueryResponse = new NeedInspectMchntStatisQueryResponse();
        needInspectMchntStatisQueryResponse.setHeader(needInspectMchntStatisQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(needInspectMchntStatisQueryRequest, NeedInspectMchntStatisQueryResponse.class);
            needInspectMchntStatisQueryResponse = (NeedInspectMchntStatisQueryResponse) dubboResult.getResult();
            needInspectMchntStatisQueryResponse.setRespCode(dubboResult.getRespCode());
            needInspectMchntStatisQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检商户统计信息查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                needInspectMchntStatisQueryResponse.setRespCode(((BizException) e).getCode());
                needInspectMchntStatisQueryResponse.setRespDesc(e.getMessage());
            } else {
                needInspectMchntStatisQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                needInspectMchntStatisQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(needInspectMchntStatisQueryResponse);
        return bspResult;
    }

    @Override
    public Result<NeedInspectMchntListQueryResponse> needInspectMchntListQueryS(NeedInspectMchntListQueryRequest needInspectMchntListQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NeedInspectMchntListQueryResponse> bspResult = new Result<>();
        NeedInspectMchntListQueryResponse needInspectMchntListQueryResponse = new NeedInspectMchntListQueryResponse();
        // 设置响应头部
        needInspectMchntListQueryResponse.setHeader(needInspectMchntListQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(needInspectMchntListQueryRequest, NeedInspectMchntListQueryResponse.class);
            needInspectMchntListQueryResponse = (NeedInspectMchntListQueryResponse) dubboResult.getResult();
            needInspectMchntListQueryResponse.setRespCode(dubboResult.getRespCode());
            needInspectMchntListQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检商户信息列表查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                needInspectMchntListQueryResponse.setRespCode(((BizException) e).getCode());
                needInspectMchntListQueryResponse.setRespDesc(e.getMessage());
            } else {
                needInspectMchntListQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                needInspectMchntListQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(needInspectMchntListQueryResponse);
        return bspResult;
    }

    @Override
    public Result<InspectCollectConfigQueryResponse> inspectCollectConfigQuery(InspectCollectConfigQueryRequest inspectCollectConfigQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<InspectCollectConfigQueryResponse> bspResult = new Result<>();
        InspectCollectConfigQueryResponse inspectCollectConfigQueryResponse = new InspectCollectConfigQueryResponse();
        inspectCollectConfigQueryResponse.setHeader(inspectCollectConfigQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(inspectCollectConfigQueryRequest, InspectCollectConfigQueryResponse.class);
            inspectCollectConfigQueryResponse = (InspectCollectConfigQueryResponse) dubboResult.getResult();
            inspectCollectConfigQueryResponse.setRespCode(dubboResult.getRespCode());
            inspectCollectConfigQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检采集配置信息查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                inspectCollectConfigQueryResponse.setRespCode(((BizException) e).getCode());
                inspectCollectConfigQueryResponse.setRespDesc(e.getMessage());
            } else {
                inspectCollectConfigQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                inspectCollectConfigQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(inspectCollectConfigQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntInspectInfoCollectResponse> mchntInspectInfoCollect(MchntInspectInfoCollectRequest mchntInspectInfoCollectRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntInspectInfoCollectResponse> bspResult = new Result<>();
        MchntInspectInfoCollectResponse mchntInspectInfoCollectResponse = new MchntInspectInfoCollectResponse();
        mchntInspectInfoCollectResponse.setHeader(mchntInspectInfoCollectRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntInspectInfoCollectRequest, MchntInspectInfoCollectResponse.class);
            mchntInspectInfoCollectResponse = (MchntInspectInfoCollectResponse) dubboResult.getResult();
            mchntInspectInfoCollectResponse.setRespCode(dubboResult.getRespCode());
            mchntInspectInfoCollectResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检任务信息采集操作失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntInspectInfoCollectResponse.setRespCode(((BizException) e).getCode());
                mchntInspectInfoCollectResponse.setRespDesc(e.getMessage());
            } else {
                mchntInspectInfoCollectResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntInspectInfoCollectResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntInspectInfoCollectResponse);
        return bspResult;
    }

    @Override
    public Result<InspectNeedAuditMchntQueryResponse> inspectNeedAuditMchntQuery(InspectNeedAuditMchntQueryRequest inspectNeedAuditMchntQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<InspectNeedAuditMchntQueryResponse> bspResult = new Result<>();
        InspectNeedAuditMchntQueryResponse inspectNeedAuditMchntQueryResponse = new InspectNeedAuditMchntQueryResponse();
        inspectNeedAuditMchntQueryResponse.setHeader(inspectNeedAuditMchntQueryRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(inspectNeedAuditMchntQueryRequest, InspectNeedAuditMchntQueryResponse.class);
            inspectNeedAuditMchntQueryResponse = (InspectNeedAuditMchntQueryResponse) dubboResult.getResult();
            inspectNeedAuditMchntQueryResponse.setRespCode(dubboResult.getRespCode());
            inspectNeedAuditMchntQueryResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("待审核巡检任务查询失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                inspectNeedAuditMchntQueryResponse.setRespCode(((BizException) e).getCode());
                inspectNeedAuditMchntQueryResponse.setRespDesc(e.getMessage());
            } else {
                inspectNeedAuditMchntQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                inspectNeedAuditMchntQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(inspectNeedAuditMchntQueryResponse);
        return bspResult;
    }

    @Override
    public Result<MchntInspectInfoAuditResponse> mchntInspectInfoAudit(MchntInspectInfoAuditRequest mchntInspectInfoAuditRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntInspectInfoAuditResponse> bspResult = new Result<>();
        MchntInspectInfoAuditResponse mchntInspectInfoAuditResponse = new MchntInspectInfoAuditResponse();
        mchntInspectInfoAuditResponse.setHeader(mchntInspectInfoAuditRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntInspectInfoAuditRequest, MchntInspectInfoAuditResponse.class);
            mchntInspectInfoAuditResponse = (MchntInspectInfoAuditResponse) dubboResult.getResult();
            mchntInspectInfoAuditResponse.setRespCode(dubboResult.getRespCode());
            mchntInspectInfoAuditResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("巡检任务审核操作失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntInspectInfoAuditResponse.setRespCode(((BizException) e).getCode());
                mchntInspectInfoAuditResponse.setRespDesc(e.getMessage());
            } else {
                mchntInspectInfoAuditResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntInspectInfoAuditResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntInspectInfoAuditResponse);
        return bspResult;
    }
}
